<?php
/**
  * Attachment.php
  *
  * This is not a freeware, use is subject to license terms
  * @author CLOUD DEV TEAM
  * 
  */
if (!defined('IN_CLOUD')) exit ('Access Denied!');

class Attachment extends Model
{
	private static $object;
	
	private function Attachment()
    {
    	$this->tableName = 'attachment';
    	parent::__construct();
    }
    
    public static function getInstance()
    {
    	if ( !is_object( self::$object ) )
    	{
    		self::$object = new Attachment();
    	}
    	return self::$object;
    }
    
	public static function initSwfUpload( $c , $a , $userId , $module , $dir = 'House' , $type = '' )
	{
		$auth_key = Common::getConfig( 'System' , 'auth_key' );
		$swfUploadKey = md5( SYSTIME . $auth_key );
		$init =  'var swfu = \'\';
		$(document).ready(function(){
		swfu = new SWFUpload({
			flash_url:"' . JSURL . 'swfupload/swfupload.swf?"+Math.random(),
			upload_url:"' . SITEURL . '/index.php?c=' . $c . '&a=' . $a . '",
			file_post_name : "Filedata",
			post_params:{"SWFUPLOADSESSID":"'. SYSTIME .'", "c":"' . $c . '", "a":"' . $a . '", "user_id":"' . $userId . '" , "module":"' . $module . '", "dir":"' . $dir . '", "type":"' . $type . '", "swfUploadKey":"' . $swfUploadKey .'"},
			file_size_limit:"' . self::getUploadMaxSize( 'K' ) . '",
			file_types:"*.gif;*.jpg;*.jpeg;*.png;*.bmp",
			file_types_description:"All Files",
			file_upload_limit:"12",
			
			custom_settings : {progressTarget : "fsUploadProgress",cancelButtonId : "btnCancel"},
	 
			button_image_url: "' . IMAGEURL . 'btn_upload.png",
			button_width: 86,
			button_height: 27,
			button_placeholder_id: "spanButtonPlaceholder",
			button_text_style: "",
			button_text_top_padding: 3,
			button_text_left_padding: 12,
			button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
			button_cursor: SWFUpload.CURSOR.HAND,

			swfupload_preload_handler : preLoad,
			swfupload_load_failed_handler : loadFailed,
			file_dialog_start_handler : fileDialogStart,
			file_queued_handler : fileQueued,
			file_queue_error_handler : fileQueueError,
			file_dialog_complete_handler : fileDialogComplete,
			upload_start_handler : uploadStart,
			upload_progress_handler : uploadProgress,
			upload_error_handler : uploadError,
			upload_success_handler : uploadSuccess,
			upload_complete_handler : uploadComplete,
			});
		});';
		return $init;
	}
	
	/**
	 * 获取缩略图列表
	 * @param array $data
	 */
	public function getThumbList( $data , $attachmentUrl = false )
	{
		$thumbList = array();
		$attachmentList = $this->select( $data );
		if ( $attachmentList && is_array( $attachmentList ) )
		{
			foreach( $attachmentList as $attachment )
			{
				if ( $attachmentUrl )
					$thumbList[] = ATTACHMENTURL . Image::getThumbPath( $attachment['filepath'] );
				else
					$thumbList[] = Image::getThumbPath( $attachment['filepath'] );
			}
		}
		return $thumbList;
	}
	
	/**
	 * 获取附件列表
	 * @param unknown_type $data
	 * @param unknown_type $attachmentUrl
	 */
	public function getAttachmentList( $data , $attachmentUrl = false )
	{
		$attachments = array();
		$attachmentList = $this->select( $data );
		if ( $attachmentList && is_array( $attachmentList ) )
		{
			foreach( $attachmentList as $attachment )
			{
				if ( $attachmentUrl )
					$attachments[] = ATTACHMENTURL . $attachment['filepath'];
				else
					$attachments[] = $attachment['filepath'];
			}
		}
		return $attachments;
	}
	
	public static function getUploadMaxSize( $type = 'M' )
	{
		$maxSize = ini_get( 'upload_max_filesize' );
		if ( $type == 'M' )
		{
			if ( strpos( $maxSize , 'M' ) !== false )
			{
				return ( int )$maxSize;
			}
			elseif ( strpos( $maxSize , 'K' ) !== false )
			{
				return ( int )( $maxSize / 1024 );
			}
		}
		elseif ( $type == "K" )
		{
			if ( strpos( $maxSize , 'M' ) !== false )
			{
				return ( int )( $maxSize * 1024 );
			}
			elseif ( strpos( $maxSize , 'K' ) !== false )
			{
				return ( int )$maxSize;
			}
		}
	}
}